from constants import *
import requests
import asyncio
import aiohttp
import re
from openpyxl import Workbook

topic_info_results = []
current_language = 'cn'


def get_url_json(url, catalog_name, object_id, language):
    """GET_CATALOG_TREE获取产品目录json原始数据"""
    data = {'language': language, 'catalogName': catalog_name, 'objectId': object_id}
    response = requests.post(url=url, headers=headers, data=data, verify=False)
    return response.json()


def get_topic_info(contents_json, catalog_name):
    """获取产品topic信息"""
    for tree in contents_json['value']['catalogTreeList']:
        get_topic(tree, 1, catalog_name)
    try:
        asyncio.get_event_loop().run_until_complete(get_href_by_object_id(catalog_name))
    except Exception as e:
        print(e)


def get_topic(tree_dict, level, catalog_name):
    """
    获取topic信息
    :param tree_dict:
    :param level:
    :return: [{'nodeName': '应用体验建议', 'level': 1, 'topic_url': '', 'sections': []}, {...}]
    """
    if level == 1:
        print(f'{tree_dict["nodeName"]} level-{level}')
        topic_info_results.append({"nodeName": tree_dict['nodeName'], 'level': level, "topic_url": ''})
    else:
        print(
            f"{tree_dict['nodeName']},level-{level},{HUAWEI_URL_PREFIX[current_language]}/{catalog_name}/{tree_dict['relateDocument']}")
        topic_info_results.append({"nodeName": tree_dict['nodeName'], 'level': level,
                                   "topic_url": f'{HUAWEI_URL_PREFIX[current_language]}/{catalog_name}/{tree_dict["relateDocument"]}'})
    if len(tree_dict['children']) != 0:
        for child in tree_dict['children']:
            get_topic(child, level + 1, catalog_name)
    else:
        return topic_info_results


async def get_href_by_object_id(catalog_name):
    # 设置允许同时运行的协程数量
    max_concurrent = MAX_SEMAPHORE
    semaphore = asyncio.Semaphore(max_concurrent)

    async with aiohttp.ClientSession() as session:
        tasks = [asyncio.create_task(get_document(session, index, topic_info, semaphore, catalog_name)) for
                 index, topic_info in enumerate(topic_info_results)]
        await asyncio.wait(tasks)


async def get_document(session, index, topic_info, semaphore, catalog_name):
    """
    异步方式获取文档内容
    :param session: aiohttp.ClientSession()
    :param index: 当前所在结果中的索引
    :param topic_info: topic相关信息
    :param semaphore: 限制线程数量
    :param catalog_name: catalog_name
    :return:
    """
    if topic_info['topic_url'] != '':
        object_id = topic_info['topic_url'].split('/')[-1]
        async with semaphore:
            current_data = {'language': current_language, 'catalogName': catalog_name, 'objectId': object_id,
                            'version': ''}

            async with session.post(url=GET_DOCUMENT_BY_ID, headers=headers, data=current_data) as response:

                res = await response.json()
                try:
                    html = res['value']['content']['content']
                except Exception as e:
                    print(e)
                    print(current_data)
                parse_html(html, index, topic_info['topic_url'])


def parse_html(html_content, index, current_url):
    """
    解析html
    :param index: 在topic_info_results中索引
    :param html_content:
    :param current_url:
    :return:
    """
    topic_info_results[index]['href'] = []
    pattern = r'<a\s+href="([^"]*)"[^>]*>([^<]*)</a>'
    matches = re.finditer(pattern, html_content)
    for match in matches:
        url = match.group(1)
        if url.startswith("#"):
            url = current_url + f'{url}'
        text = match.group(2)
        topic_info_results[index]['href'].append({text: url})
        print(url)


def write_to_excel(results):
    wb = Workbook()
    my_sheet = wb.worksheets[0]
    max_level = 0
    for topic_info in results:
        if topic_info['level'] >= max_level:
            max_level = topic_info['level']
    first_row = []
    for i in range(max_level):
        first_row.append(f'topic_level{i + 1}')

    first_row.append('topic地址')
    first_row.append('超链接信息')
    first_row.append('超链接地址')
    my_sheet.append(first_row)
    for topic_info in results:
        if topic_info['topic_url'] == "":
            my_sheet.append([topic_info['nodeName']])
        else:
            topic_level = topic_info['level']
            current_row = [''] * (topic_level - 1)
            current_row.append(topic_info['nodeName'])
            current_row = current_row + [''] * (max_level - topic_level) + [topic_info['topic_url']]

            for href in topic_info['href']:
                for k, v in href.items():
                    current_row = current_row + [k] + [v]
                    my_sheet.append(current_row)
                    topic_level = topic_info['level']
                    current_row = [''] * (topic_level - 1)
                    current_row.append(topic_info['nodeName'])
                    current_row = current_row + [''] * (max_level - topic_level) + [topic_info['topic_url']]
    wb.save(f'超链接_.xlsx')


def main():
    global current_language, topic_info_results
    result = []
    current_language = 'cn'  # 需要统计的产品中文或者英文
    products = [Guides, Api, FAQ, BestPractices, Overview]  # 将需要统计的产品类型加入到列表中
    # products = [Overview]  # 将需要统计的产品类型加入到列表中
    for product in products:
        contents_json = get_url_json(GET_CATALOG_TREE, product.CATALOG_NAME, product.OBJECT_ID, current_language)
        get_topic_info(contents_json, product.CATALOG_NAME)
        result += topic_info_results
        topic_info_results = []
    write_to_excel(result)


if __name__ == '__main__':
    main()
